﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Xml;

namespace FileBrowser
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            LoadTreeView();
        }

        // 加载树形视图，展示用户目录
        private void LoadTreeView()
        {
            var directories = Directory.GetDirectories(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile));

            foreach (var directory in directories)
            {
                TreeViewItem rootItem = new TreeViewItem
                {
                    Header = new DirectoryInfo(directory).Name,
                    Tag = directory
                };
                rootItem.Items.Add(null);  // Placeholder for lazy loading
                rootItem.Expanded += Folder_Expanded;
                treeView.Items.Add(rootItem);
            }
        }

        // 树形视图展开事件，加载子文件夹
        private void Folder_Expanded(object sender, RoutedEventArgs e)
        {
            TreeViewItem treeViewItem = e.OriginalSource as TreeViewItem;
            if (treeViewItem != null && treeViewItem.Items.Count == 1 && treeViewItem.Items[0] == null)
            {
                treeViewItem.Items.Clear();

                string path = treeViewItem.Tag.ToString();
                var subDirectories = Directory.GetDirectories(path);

                foreach (var subDirectory in subDirectories)
                {
                    TreeViewItem subItem = new TreeViewItem
                    {
                        Header = new DirectoryInfo(subDirectory).Name,
                        Tag = subDirectory
                    };
                    subItem.Items.Add(null);
                    subItem.Expanded += Folder_Expanded;
                    treeViewItem.Items.Add(subItem);
                }
            }
        }

        // 树形视图选择事件，显示文件夹中的文件和文件夹
        private void TreeView_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (treeView.SelectedItem is TreeViewItem selectedItem)
            {
                string path = selectedItem.Tag.ToString();
                LoadListView(path);
            }
        }

        // 加载列表视图，显示目录下的文件和子文件夹
        private void LoadListView(string path)
        {
            listView.Items.Clear();

            // 加载文件夹
            var directories = Directory.GetDirectories(path);
            foreach (var dir in directories)
            {
                listView.Items.Add(new { Name = new DirectoryInfo(dir).Name, Type = "Folder" });
            }

            // 加载文件
            var files = Directory.GetFiles(path);
            foreach (var file in files)
            {
                listView.Items.Add(new { Name = Path.GetFileName(file), Type = "File" });
            }
        }

        // 双击列表项，提取 XML 文件内容
        private void ListView_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            if (listView.SelectedItem is dynamic selectedItem)
            {
                string filePath = Path.Combine(((TreeViewItem)treeView.SelectedItem).Tag.ToString(), selectedItem.Name.ToString());

                if (Path.GetExtension(filePath).ToLower() == ".xml")
                {
                    ExtractXmlContent(filePath);
                }
            }
        }

        // 提取 XML 文件内容
        private void ExtractXmlContent(string filePath)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(filePath);

                string xmlContent = $"Root Node: {xmlDoc.DocumentElement.Name}\n";
                foreach (XmlNode node in xmlDoc.DocumentElement.ChildNodes)
                {
                    xmlContent += $"Node: {node.Name}, Attributes: {node.Attributes?.Count}, Text: {node.InnerText}\n";
                }

                MessageBox.Show(xmlContent, "XML Content", MessageBoxButton.OK, MessageBoxImage.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"Error reading XML: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        // 刷新按钮点击事件
        private void RefreshButton_Click(object sender, RoutedEventArgs e)
        {
            LoadTreeView();
        }

        // 退出菜单点击事件
        private void ExitMenuItem_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}